<?php 
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 5112 2010-08-30 02:53:12Z leha $
 * @since		2.0.0
 */
?>
<?php 
$this->headTitle($this->translator('poll_list_page_title'));

$this->headLink()
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.core.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.resizable.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.dialog.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.theme.css');

$this->headScript()
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.core.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.draggable.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.resizable.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.dialog.js');
?>

<div class="grid_12 t_a_ui_breadcrumb">
	<span><?php echo $this->translator('poll_list_breadcrumb'); ?></span>
</div>

<div class="grid_12"><?php echo $this->flashMessenger(); ?></div>

<div class="grid_12 t_a_bottom">
	<div class="grid_9 alpha">
		<?php if ($this->numQuestions == 0) : ?>
		<?php echo $this->translator('poll_list_not_found'); ?>
		<?php else : ?>	
		<?php echo $this->translator('poll_list_guide'); ?>
		<?php endif; ?>
	</div>
	
	<div class="grid_3 omega">
		<div class="t_a_right">	
			<a href="<?php echo $this->url(array(), 'poll_question_add'); ?>" class="t_a_ui_button_link"><span><?php echo $this->translator('poll_add_breadcrumb'); ?></span></a>
		</div>
	</div>
</div>

<!-- List of questions -->
<?php if ($this->numQuestions == 0) : ?>
<div class="grid_12 t_a_bottom">
	<?php 
	/**
	 * Show language switcher
	 * @since 2.0.8
	 */
	?>
	<?php echo $this->languageSwitcher('flag', str_repeat('&nbsp;', 2)); ?>
</div>
<?php else : ?>
	<div class="grid_12 t_a_ui_data_list_header">
		<div class="grid_7 alpha">
			<div><?php echo $this->translator('poll_title'); ?></div>
		</div>
		
		<div class="grid_2"><?php echo $this->translator('poll_status'); ?></div>
		
		<div class="grid_2"><?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?></div>
		
		<div class="grid_1 omega"><?php echo $this->translator('poll_id'); ?></div>
	</div>

	<div class="grid_12">
		<?php foreach ($this->questions as $question) : ?>
		<div id="questionRow_<?php echo $question->question_id; ?>" class="clearfix t_a_ui_data_list_content">
			<div class="grid_7 alpha">
				<div>
					<?php echo sprintf($this->translator('poll_list_title'), $question->title, $question->content, date('Y-m-d H:i:s',strtotime($question->created_date))); ?>
				</div>
				
				<div class="t_a_top t_a_ui_helper_action">
					<?php if ($this->allow('edit')) : ?>
					<a href="<?php echo $this->url(array('question_id' => $question->question_id), 'poll_question_edit'); ?>"><?php echo $this->translator('poll_list_action_edit'); ?></a> |
					<?php endif; ?>
					
					<?php if ($this->allow('delete')) : ?> 
					<a class="deleteAction" rel="<?php echo $question->question_id; ?>" href="javascript: void(0);"><?php echo $this->translator('poll_list_action_delete'); ?></a> |
					<?php endif; ?>
					
					<?php if ($this->allow('activate')) : ?>
					<a class="activateAction" rel="<?php echo $question->question_id; ?>_<?php echo $question->is_active; ?>" href="javascript: void(0);"><?php if ($question->is_active == 1) : ?><?php echo $this->translator('poll_list_action_deactivate'); ?><?php else : ?><?php echo $this->translator('poll_list_action_activate'); ?><?php endif; ?></a>
					<?php endif; ?>
				</div>
			</div>
			
			<div class="grid_2" id="questionStatus_<?php echo $question->question_id; ?>">
				<?php if ($question->is_active == 1) : ?>
					<?php echo $this->translator('poll_list_status_activated'); ?>
				<?php elseif ($question->is_active == 0 || null == $question->status) : ?>
					<?php echo $this->translator('poll_list_status_not_activated'); ?>
				<?php endif; ?>
			</div>
			
			<?php 
			/**
			 * @since 2.0.8
			 */
			?>
			<div class="grid_2">
			<?php if (($translations = $this->translationItems($question)) != null) : ?>
				<?php foreach ($translations as $language => $translation) : ?>
					<?php if ($translation == null && $language != $question->language) : ?>
						<?php if (isset($translations[$this->APP_DEFAULT_LANG])) : ?>
						<a class="t_a_ui_helper_action_add" href="<?php echo $this->url(array('source_id' => $translations[$this->APP_DEFAULT_LANG]->question_id), 'poll_question_add_localization'); ?>/<?php echo $language; ?>/">&nbsp;</a>
						<?php else : ?>
						<a class="t_a_ui_helper_action_add" href="<?php echo $this->url(array(), 'poll_question_add'); ?>/<?php echo $language; ?>/">&nbsp;</a>
						<?php endif; ?>
				
					<?php elseif (($translation == null && $language == $question->language) || ($translation != null && $translation->question_id == $question->question_id)) : ?>
					<a class="t_a_ui_helper_action_edit" href="<?php echo $this->url(array('question_id' => $question->question_id), 'poll_question_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
					
					<?php elseif ($translation != null && $translation->question_id != $question->question_id) : ?>
					<a class="t_a_ui_helper_action_edit" href="<?php echo $this->url(array('question_id' => $translation->question_id), 'poll_question_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
					<?php endif; ?>
				<?php endforeach; ?>
			<?php else : ?>
			&nbsp;
			<?php endif; ?>
			</div>
			
			<div class="grid_1 omega"><?php echo $question->question_id; ?></div>
		</div>
		<?php endforeach; ?>
	</div>
	
	<div class="grid_12 t_a_bottom t_a_ui_paginator">
		<?php echo $this->paginator()->slide($this->paginator, $this->paginatorOptions); ?>
	</div>
<?php endif; ?>

<div id="messageDialog"></div>

<div id="confirmDialog"></div>

<script type="text/javascript">
$(document).ready(function() {
	<?php if ($this->allow('delete')) : ?>
	$('a.deleteAction').click(function() {
		var self = this;
		$('#confirmDialog').html('<?php echo addslashes($this->translator('poll_list_action_delete_confirm')); ?>')
			.dialog({
				title: '<?php echo addslashes($this->translator('common_dialog_confirm')); ?>',
				dialogClass: 't_a_ui_dialog',
				modal: true,
				closeOnEscape: false,
				resizable: false,
				open: function(event, ui) {
					$('.ui-dialog-titlebar-close').hide();
					$(this).parent().find('.ui-dialog-buttonpane button').wrapInner('<span/>');
				},
				buttons: {
					'<?php echo addslashes($this->translator('common_dialog_confirm_no')); ?>': function() {
						$(this).dialog('destroy');
					},
					'<?php echo addslashes($this->translator('common_dialog_confirm_yes')); ?>': function() {
						$(this).dialog('destroy');
						$(self).html('').addClass('t_a_ui_helper_loading');
						var id = $(self).attr('rel');
						$.ajaxq('poll_question', {
							type: 'POST',
							url: "<?php echo $this->url(array(), 'poll_question_delete'); ?>",
							data: { id: id },
							success: function(response) {
								if ('RESULT_OK' == response) {
									$('#questionRow_' + id).fadeOut('slow');
								}
							}
						});
					}
				}
			});
	});
	<?php endif; ?>
	
	<?php if ($this->allow('activate')) : ?>
	$('a.activateAction').click(function() {
		var self = this;
		var arr = $(this).attr('rel').split('_');
		$(self).html('').addClass('t_a_ui_helper_loading');
		$.ajaxq('poll_question', {
			url: "<?php echo $this->url(array(), 'poll_question_activate'); ?>",
			type: 'POST',
			data: { id: arr[0], status: arr[1] },
			success: function(response) {
				$(self).attr('rel', arr[0] + '_' + response);
				if (response == 1) {
					$('#questionStatus_' + arr[0]).html("<?php echo $this->translator('poll_list_status_activated'); ?>");
					$(self).html("<?php echo $this->translator('poll_list_action_deactivate'); ?>");
				} else {
					$('#questionStatus_' + arr[0]).html("<?php echo $this->translator('poll_list_status_not_activated'); ?>");
					$(self).html("<?php echo $this->translator('poll_list_action_activate'); ?>");
				}
				$(self).removeClass('t_a_ui_helper_loading');

				$('#messageDialog').html((response == 'active') ? '<?php echo addslashes($this->translator('poll_list_activated_success')); ?>' : '<?php echo addslashes($this->translator('poll_list_deactivated_success')); ?>')
					.dialog({
						title: '<?php echo addslashes($this->translator('common_dialog_message')); ?>',
						dialogClass: 't_a_ui_dialog',
						modal: true,
				    	show: { effect: 'fade' },
						open: function(event, ui) {
							setTimeout(function() {
								$('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
							}, 3000);
						}
					});
			}
		});
	});
	<?php endif; ?>
});
</script>
